package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    public int insertUser(User user) {
        System.out.println("UserMapper:将用户信息插入到数据库中...");
        try (Connection connection = dbUtil.getConnection();) {
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public User selectUserByUsername(String username) {
        System.out.println("用户登陆中....");
        try (Connection connection = dbUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();


            if (rs.next()) {
                User user = new User();
                user.setId( rs.getInt("id"));
                user.setUsername( rs.getString("username"));
                user.setPassword( rs.getString("password"));
                user.setNickname( rs.getString("nickname"));
                user.setAge( rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<User> selectAllUser() {
        System.out.println("查询用户列表中....");
        try (Connection connection = dbUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<User> list = new ArrayList<>();


            while(rs.next()) {
                User user = new User();
                user.setId( rs.getInt("id"));
                user.setUsername( rs.getString("username"));
                user.setPassword( rs.getString("password"));
                user.setNickname( rs.getString("nickname"));
                user.setAge( rs.getInt("age"));
                list.add(user);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateUser(User user) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "update user set password = ?,nickname = ?,age = ? " +
                         "where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setInt(4,user.getId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;

    }


//    public ResultSet selectUser(User user) {
////        System.out.println("UserMapper:查询用户信息中...");
////        try (Connection connection = dbUtil.getConnection();) {
////            String sql = "SELECT username,password,nickname,age " +
////                    "FROM user WHERE username = ? AND password = ?";
////            PreparedStatement ps = connection.prepareStatement(sql);
////            ps.setString(1, user.getUsername());
////            ps.setString(2, user.getPassword());
////
////            ResultSet rs = ps.executeQuery();
////            try {
////                if (rs.next()) {
////                    System.out.println("登录成功！");
////                } else {
////                    System.out.println("登录失败！");
////                }
////            } catch (SQLException e) {
////                e.printStackTrace();
////            }
////            return ps.executeQuery();
////        } catch (SQLException e) {
////            e.printStackTrace();
////        }
////    return null;
////    }

}